#ifndef MAINSCENE_H
#define MAINSCENE_H
enum MainBtn
{
    ABOUT_MB,
    START_MB,
    HELP_MB,
    EXIT_MB,
    NULL_MB,
    YES_MB,
    NO_MB,
}mainBtn(NULL_MB);

class MainScene : public QGraphicsScene
{
public:
    explicit MainScene(QObject *parent = 0):
        QGraphicsScene(parent)
    {
        setSceneRect(0.0, 0.0, 640.0, 360.0);

        m_mbgItem = new ImageItem(QImage(":/image/main/menu_bg.png"),
                                 QRect(QPoint(0,0),QImage(":/image/main/menu_bg.png").size()));
        m_mbgItem->setZValue(0.0);
        m_mbgItem->setPos(0.0,0.0);
        m_mbgItem->setVisible(true);
        addItem(m_mbgItem);

        m_aboutItem = new ImageItem(QImage(":/image/main/menu_about.png"),
                                    QRect(QPoint(0,0),QImage(":/image/main/menu_about.png").size()));
        m_aboutItem->setZValue(1.0);
        m_aboutItem->setPos(0.0,307.0);
        addItem(m_aboutItem);

        m_oviItem = new ImageItem(QImage(":/image/main/menu_ovi.png"),
                                  QRect(QPoint(0,0),QImage(":/image/main/menu_ovi.png").size()));
        m_oviItem->setZValue(1.0);
        m_oviItem->setPos(53.0,307.0);
        addItem(m_oviItem);

        if(resources::m_bplaysound)
        {
            m_soundItem = new ImageItem(*resources::m_mainSoundImage,
                                        QRect(QPoint(0,0),resources::m_mainSoundImage->size()));
        }
        else
        {
            m_soundItem = new ImageItem(*resources::m_mainSoundsImage,
                                        QRect(QPoint(0,0),resources::m_mainSoundsImage->size()));
        }
        m_soundItem->setZValue(1.0);
        m_soundItem->setPos(100,307);
        addItem(m_soundItem);

        switch(resources::m_iLanguage)
        {
        case QLocale::Chinese:
        case QLocale::HongKong:
        case QLocale::Taiwan:
        default:
        {
            m_startItem = new ImageItem(*resources::m_mainStartImage,
                                        QRect(QPoint(0,0),resources::m_mainStartImage->size()));
            m_helpItem = new ImageItem(*resources::m_mainHelpImage,
                                       QRect(QPoint(0,0),resources::m_mainHelpImage->size()));
            m_exitItem = new ImageItem(*resources::m_mainExitImage,
                                       QRect(QPoint(0,0),resources::m_mainExitImage->size()));
        }
        break;
        }

        m_startItem->setZValue(1.0);
        m_startItem->setPos(425.0,39.0);
        addItem(m_startItem);

        m_helpItem->setZValue(1.0);
        m_helpItem->setPos(425.0,110.0);
        addItem(m_helpItem);

        m_exitItem->setZValue(1.0);
        m_exitItem->setPos(425.0,181.0);
        addItem(m_exitItem);

        m_promptBgItem = new ImageItem(QImage(":/image/prompt/tc.png"),
                                       QRect(QPoint(0,0),QImage(":/image/prompt/tc.png").size()));
        m_promptBgItem->setZValue(2.0);
        m_promptBgItem->setVisible(false);
        addItem(m_promptBgItem);

        m_promptYesItem = new ImageItem(QImage(":/image/prompt/y_170,192.png"),
                                        QRect(QPoint(0,0),QImage(":/image/prompt/y_170,192.png").size()));
        m_promptYesItem->setPos(180,201);
        m_promptYesItem->setZValue(3.0);
        m_promptYesItem->setVisible(false);
        addItem(m_promptYesItem);

        m_promptNoItem = new ImageItem(QImage(":/image/prompt/n_376,192.png"),
                                       QRect(QPoint(0,0),QImage(":/image/prompt/n_376,192.png").size()));
        m_promptNoItem->setPos(388,201);
        m_promptNoItem->setZValue(3.0);
        m_promptNoItem->setVisible(false);
        addItem(m_promptNoItem);
    }
    ~MainScene()
    {
    }

    void mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        if(m_promptBgItem->isVisible())
        {
            if(m_promptYesItem->isUnderMouse())
            {
                if(resources::m_bplaysound)
                {
                    resources::m_sound->play(5);
                }
                m_promptYesItem->setVisible(true);
                mainBtn = YES_MB;
            }
            else if(m_promptNoItem->isUnderMouse())
            {
                if(resources::m_bplaysound)
                {
                    resources::m_sound->play(5);
                }
                m_promptNoItem->setVisible(true);
                mainBtn = NO_MB;
            }
        }
        else
        {
            if(m_startItem->isUnderMouse())
            {
                if(resources::m_bplaysound)
                {
                    resources::m_sound->play(5);
                }
                m_startItem->setImage(*resources::m_mainStartpImage);
                mainBtn = START_MB;
            }
            else if(m_exitItem->isUnderMouse())
            {
                if(resources::m_bplaysound)
                {
                    resources::m_sound->play(5);
                }
                m_exitItem->setImage(*resources::m_mainExitpImage);
                mainBtn = EXIT_MB;
            }
            else if(m_aboutItem->isUnderMouse())
            {
                if(resources::m_bplaysound)
                {
                    resources::m_sound->play(5);
                }
                resources::SetHelp(false);
            }
            else if(m_helpItem->isUnderMouse())
            {
                if(resources::m_bplaysound)
                {
                    resources::m_sound->play(5);
                }
                m_helpItem->setImage(*resources::m_mainHelppImage);
                mainBtn = HELP_MB;
            }
            else if(m_oviItem->isUnderMouse())
            {
                if(resources::m_bplaysound)
                {
                    resources::m_sound->play(5);
                }
                if(!QDesktopServices::openUrl(QUrl("http://store.ovi.mobi",QUrl::TolerantMode)))
                {

                    QMessageBox msgBox;

                    msgBox.setText("Fail to connect Ovi,please check network connections!");

                    msgBox.setStandardButtons(QMessageBox::Yes);
                    msgBox.setDefaultButton(QMessageBox::Yes);
                    msgBox.exec();
                }
            }
            else if(m_soundItem->isUnderMouse())
            {
                resources::m_bplaysound = !resources::m_bplaysound;
                resources::SaveData();
                if(resources::m_bplaysound)
                {
                    m_soundItem->setImage(*resources::m_mainSoundImage);
                }
                else
                {
                    m_soundItem->setImage(*resources::m_mainSoundsImage);
                }
                if(resources::m_bplaysound)
                {
                    resources::m_sound->play(5);
                    resources::m_sound->playBgSound(0);
                }
                else
                {
                    resources::m_sound->stop(0);
                }
            }
        }

    }
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
        switch(mainBtn)
        {
        case START_MB:
        {
            m_startItem->setImage(*resources::m_mainStartImage);
            if(m_startItem->isUnderMouse())
            {
                resources::SetSel();
            }
        }
        break;
        case EXIT_MB:
        {
            m_exitItem->setImage(*resources::m_mainExitImage);
            if(m_exitItem->isUnderMouse())
            {
                m_promptBgItem->setVisible(true);
//                QApplication::instance()->exit(0);
            }
        }
        break;
        case HELP_MB:
        {
            m_helpItem->setImage(*resources::m_mainHelpImage);
            if(m_helpItem->isUnderMouse())
            {
                resources::SetHelp(true);
            }
        }
        break;
        case YES_MB:
        {
            m_promptYesItem->setVisible(false);
            if(m_promptYesItem->isUnderMouse())
            {
               QApplication::instance()->exit(0);
            }
        }
        break;
        case NO_MB:
        {
            m_promptNoItem->setVisible(false);
            if(m_promptNoItem->isUnderMouse())
            {
                m_promptYesItem->setVisible(false);
                m_promptBgItem->setVisible(false);
            }
        }
        break;
        default:
            break;
        }
    }
public:
    ImageItem*      m_mbgItem;
    ImageItem*      m_aboutItem;
    ImageItem*      m_startItem;
    ImageItem*      m_helpItem;
    ImageItem*      m_exitItem;
    ImageItem*      m_oviItem;
    ImageItem*      m_soundItem;

    ImageItem*      m_promptBgItem;
    ImageItem*      m_promptYesItem;
    ImageItem*      m_promptNoItem;
};

#endif // MAINSCENE_H
